package com.yumi.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yumi.entity.TrainingRecord;
import com.yumi.mapper.TrainingRecordMapper;
import com.yumi.service.TrainingRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 培训记录服务实现类
 */
@Service
@Slf4j
public class TrainingRecordServiceImpl extends ServiceImpl<TrainingRecordMapper, TrainingRecord> implements TrainingRecordService {

    /**
     * 添加培训记录
     * @param trainingRecord 培训记录信息
     * @return 是否添加成功
     */
    @Override
    public boolean addTrainingRecord(TrainingRecord trainingRecord) {
        log.info("添加培训记录：{}", trainingRecord);
        return save(trainingRecord);
    }

    /**
     * 批量添加培训记录
     * @param trainingRecords 培训记录列表
     * @return 是否添加成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchAddTrainingRecords(List<TrainingRecord> trainingRecords) {
        log.info("批量添加培训记录，数量：{}", trainingRecords.size());
        return saveBatch(trainingRecords);
    }

    /**
     * 修改培训记录
     * @param trainingRecord 培训记录信息
     * @return 是否修改成功
     */
    @Override
    public boolean updateTrainingRecord(TrainingRecord trainingRecord) {
        log.info("修改培训记录：{}", trainingRecord);
        return updateById(trainingRecord);
    }

    /**
     * 删除培训记录
     * @param id 记录ID
     * @return 是否删除成功
     */
    @Override
    public boolean deleteTrainingRecord(Long id) {
        log.info("删除培训记录，ID：{}", id);
        return removeById(id);
    }

    /**
     * 根据ID查询培训记录
     * @param id 记录ID
     * @return 培训记录信息
     */
    @Override
    public TrainingRecord getTrainingRecordById(Long id) {
        return getById(id);
    }

    /**
     * 分页查询培训记录
     * @param page 分页信息
     * @param trainingId 培训计划ID
     * @param employeeId 员工ID
     * @return 培训记录分页结果
     */
    @Override
    public Page<TrainingRecord> getTrainingRecordPage(Page<TrainingRecord> page, Long trainingId, Long employeeId) {
        LambdaQueryWrapper<TrainingRecord> queryWrapper = new LambdaQueryWrapper<>();
        
        // 根据条件构建查询
        if (trainingId != null) {
            queryWrapper.eq(TrainingRecord::getTrainingId, trainingId);
        }
        
        if (employeeId != null) {
            queryWrapper.eq(TrainingRecord::getEmployeeId, employeeId);
        }
        
        // 按创建时间降序排序
        queryWrapper.orderByDesc(TrainingRecord::getCreateTime);
        
        return page(page, queryWrapper);
    }
} 